package com.oracletomysql;

import com.oracletomysql.pojo.BaseGnlArv;
import com.oracletomysql.utils.DateUtil;
import com.oracletomysql.utils.MysqlGetConnection;
import com.oracletomysql.utils.OracleGetConnection;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author 薛虎
 * @date 2021/1/20 13:31
 * 数字化加工全宗数据迁移
 * 退休档案
 * 档案科
 * 企业档案
 */
public class OracleToMysql {

    public static void main(String[] args) {

        Connection connection = null;
        try {

            String sql = "SELECT a.*,b.ORGNAM FROM BAS_GNLARV a, SYS_ORG b where a.ORGID = b.ORGID ";
            QueryRunner runner = new QueryRunner();
            MapListHandler handler = new MapListHandler();
            connection = OracleGetConnection.getOracleConnection();
            List<Map<String, Object>> list = runner.query(connection, sql, handler);

            list.forEach(System.out::println);

            BaseGnlArv baseGnlArv = new BaseGnlArv();
            long nowSecond = DateUtil.nowSecond();

            for (Map<String, Object> s : list) {

                baseGnlArv.setGnlArvId((String) s.get("GNLARVID"));
                baseGnlArv.setOrgId((String) s.get("ORGID"));
                baseGnlArv.setGnlArvCode((String) s.get("GNLARVCOD"));
                baseGnlArv.setOrgName((String) s.get("ORGNAM"));
                baseGnlArv.setSeqNo(Integer.valueOf(s.get("SEQNO").toString()).intValue());
                baseGnlArv.setDescr((String) s.get("DESCR"));

                baseGnlArv.setGnlArvName((String) s.get("GNLARVNAM"));
                baseGnlArv.setUpdateTime(nowSecond);
                baseGnlArv.setCreateTime(nowSecond);
            }


            getMysqlConnection(baseGnlArv);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (!connection.isClosed()) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


    public static void getMysqlConnection(BaseGnlArv baseGnlArv) {
        Connection connection = null;
        try {
            connection.setAutoCommit(false);
            QueryRunner runner = new QueryRunner();
            connection = MysqlGetConnection.getMysqlConnection();
            String sql = "insert into base_gnl_arv(gnl_arv_id,gnl_arv_code,gnl_arv_name,org_id,org_name,descr,seq_no,create_time,update_time)" +
                    "values(?,?,?,?,?,?,?,?,?)";
            int insertCount
                    = runner.update(connection, sql, baseGnlArv.getGnlArvId(), baseGnlArv.getGnlArvCode(), baseGnlArv.getGnlArvName(), baseGnlArv.getOrgId(), baseGnlArv.getOrgName(), baseGnlArv.getDescr(), baseGnlArv.getSeqNo(), baseGnlArv.getCreateTime(), baseGnlArv.getCreateTime());
            connection.commit();
            System.out.println("添加了" + insertCount + "条记录");

        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                if (!connection.isClosed()) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }

}
